package com.example.demo.command;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ch.selise.exercise.pattern.notepad.storage.AppStorageSingleton;

import com.example.demo.delegates.EventCallBack;
import com.example.demo.views.DataEntryTable;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Table;

public abstract class ActionCommand implements ClickListener {

	private static final long serialVersionUID = -6077073239872940767L;
	private String command = null;
	private EventCallBack delegate = null;
	private Table tableX = null;
	
	public ActionCommand(String command) {
		super();
		this.command = command;
	}
	
	public ActionCommand(String command, Table table) {
		this(command);
		this.tableX = table;
	}
	
	abstract protected void postClickAction(ClickEvent event);
	
	@Override
	public void buttonClick(ClickEvent event) {
		
		System.out.println(command);
		
		if(this.delegate != null){
			this.delegate.pleaseUpdate("Save");
		}
		
		postClickAction(event);
	}

	public EventCallBack getDelegate() {
		return delegate;
	}

	public void setDelegate(EventCallBack delegate) {
		this.delegate = delegate;
	}

	public String getCommand() {
		return command;
	}

	public void setCommand(String command) {
		this.command = command;
	}
	
	public Table getTableX() {
		return tableX;
	}

	public void setTableX(Table tableX) {
		this.tableX = tableX;
	}
	
	protected void reloadTable() {
		
		//Loading data if refresh.
		((DataEntryTable) getTableX()).deleteAllItems(false);
		List<Map<String, Object>> dataSet = new ArrayList<Map<String,Object>>();		
		for (String entry : AppStorageSingleton.getInstance().getText()) {
			Map<String, Object> x = new HashMap<String, Object>();
			x.put("Text", entry);
			x.put("Edit", "Edit");
			x.put("Delete", "Del");
			dataSet.add(x);
		}
		((DataEntryTable) getTableX()).loadOrderdData(dataSet, false);
		//
	}
}
